import _ from 'lodash';

import { LayoutType } from '@/customTypes/customTypes';

import { SYS_INFO_KEY } from './constant';

/**
 * 设置系统信息
 * @param { Record<string, any> } sysInfo
 * @default {}
 */
export const setCurrentSys = (sysInfo: Record<string, any> = {}) => {
  window.sessionStorage.setItem(SYS_INFO_KEY, JSON.stringify(sysInfo));
};

/**
 * 设置系统-layout类型信息
 * @param { LayoutType } layoutType
 * @default 'mix'
 */
export const setCurrentSysLayoutType = (layoutType: LayoutType = 'mix') => {
  window.sessionStorage.setItem('rbLayoutType', layoutType);
};

/**
 * 获取系统信息
 * @return { Record<string, any> } sysInfo
 */
export const getCurrentSys = (): Record<string, any> => {
  return JSON.parse(window.sessionStorage.getItem(SYS_INFO_KEY) || '{}');
};

/**
 * 获取系统-layout类型信息
 * @return { LayoutType } layoutType
 */
export const getCurrentSysLayoutType = (): LayoutType => {
  return (window.sessionStorage.getItem('rbLayoutType') || 'mix') as LayoutType;
};

/**
 * 删除系统信息
 */
export const removeCurrentSys = () => {
  window.sessionStorage.removeItem(SYS_INFO_KEY);
};

/**
 * 判断有无系统信息
 * @return { boolean } true | false
 */
export const hasCurrentSys = (): boolean => {
  return !_.isEmpty(getCurrentSys());
};
